package webservices;

import interfaces.IFavouriteManage;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import webservices.helper.Helper;

public class FavouriteManageImpl implements IFavouriteManage {
	
	public String update(String action, String courseID, String userID){
		String returnMessage = "";
		Connection con = null;
		try {
			con = Helper.getDBConnection();
			
			if (action.equals("add")){
				PreparedStatement pst = con.prepareStatement("insert into favourite(courseid, userid) values(?, ?)");
				pst.setString(1, courseID);
				pst.setString(2, userID);
				pst.executeUpdate();
			}else if (action.equals("delete")){
				PreparedStatement pst = con.prepareStatement("delete from favourite where courseid = ? and userid = ?");
				pst.setString(1, courseID);
				pst.setString(2, userID);
				pst.executeUpdate();
			}
			returnMessage = "<response name='success' />";
		} catch (SQLException e) {
			e.printStackTrace();
			returnMessage = "<response name='fail' value ='收藏失败：" + e.getLocalizedMessage() + "' />";
		} finally {
			try{
				con.close();
			}catch(SQLException e) {}
		}
		return returnMessage;
	}
	
}